﻿Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack = False Then
            CargarDataListPaises()
            cargarGrillaProvincias()
        End If
    End Sub

    Sub cargarGrillaProvincias()

        gvProvincias.DataSource = ProvinciasDB.GetProvinciasDePais(ddlPaises.SelectedValue)
        gvProvincias.DataBind()
    End Sub

    Sub CargarDataListPaises()
        Dim ds As New DataSet
        ds = PaisesDB.GetPaises
        ddlPaises.DataSource = ds.Tables("Paises")
        ddlPaises.DataValueField = "Id_pais"
        ddlPaises.DataTextField = "Pais"
        ddlPaises.DataBind()
        ddlPaises.Items.Insert(0, New ListItem("Todos", "0"))
        ddlPaises.SelectedValue = 1

    End Sub

    Protected Sub gvProvincias_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvProvincias.PageIndexChanging
        gvProvincias.PageIndex = e.NewPageIndex
        cargarGrillaProvincias()
    End Sub

    Protected Sub gvProvincias_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvProvincias.SelectedIndexChanged
        txtidProvincia.Text = gvProvincias.SelectedDataKey.Values(0).ToString()
        txtProvincia.Text = gvProvincias.SelectedRow.Cells(1).Text.ToString()
        btnAceptar.Enabled = True

    End Sub
    Protected Sub btnNuevo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        btnNuevo.Enabled = False
        btnAceptar.Enabled = True
        gvProvincias.Enabled = False
        btnCancelar.Enabled = True
        Limpiar()
    End Sub

    Private Sub Limpiar()
        txtProvincia.Text = ""
        txtidProvincia.Text = ""
    End Sub
    Private Sub BotonesInicial()
        btnNuevo.Enabled = True
        btnAceptar.Enabled = False
        gvProvincias.Enabled = True
        gvProvincias.SelectedIndex = -1
    End Sub


    Protected Sub gvPaises_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvProvincias.SelectedIndexChanged
        txtidProvincia.Text = gvProvincias.SelectedDataKey.Values(0).ToString()
        txtProvincia.Text = gvProvincias.SelectedRow.Cells(1).Text.ToString()
        btnAceptar.Enabled = True
        ddlPaises.SelectedValue = gvProvincias.SelectedDataKey.Values(1).ToString()
    End Sub

    Protected Sub btnAceptar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAceptar.Click
        Try
            If btnNuevo.Enabled = False Then 'es un nuevo registro
                ProvinciasDB.InsertarProvincia(txtProvincia.Text, Integer.Parse(ddlPaises.SelectedValue))
            Else 'es una actualizacion
                ProvinciasDB.ActualizarProvincia(Integer.Parse(txtidProvincia.Text.ToString()), txtProvincia.Text.ToString())
            End If
            cargarGrillaProvincias()
            Limpiar()
            BotonesInicial()
        Catch ex As Exception
            Session("Error") = ex.Message.ToString()
            Session("Pagina") = "Provincias.aspx"
            Server.Transfer("Errores.aspx")
        End Try

    End Sub

    Protected Sub btnCancelar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        txtProvincia.Text = ""
        txtidProvincia.Text = ""
        btnNuevo.Enabled = True
        btnAceptar.Enabled = False
        gvProvincias.Enabled = True
        gvProvincias.SelectedIndex = -1
    End Sub

    Protected Sub gvPaises_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvProvincias.RowDeleting
        Try
            ProvinciasDB.EliminarProvincia(Convert.ToInt32(gvProvincias.DataKeys(e.RowIndex).Value.ToString()))
        Catch ex As Exception
            Session("Error") = ex.Message.ToString
            Session("Pagina") = "Provincias.aspx"
            Server.Transfer("Errores.aspx")
        End Try

        cargarGrillaProvincias()

    End Sub

    Protected Sub ddlPaises_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlPaises.SelectedIndexChanged
        cargarGrillaProvincias()
    End Sub
End Class
